package com.example.firstapp.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.firstapp.R;
import com.example.firstapp.entity.Domain.FoodType;
import com.example.firstapp.entity.Restaurant;
import com.example.firstapp.entity.RestaurantReview;

public class RestaurantAdapter extends ArrayAdapter<Restaurant> {

	private int resource;
	private Context context;
	private List<Restaurant> data;

	public RestaurantAdapter(Context context, int resource,
			List<Restaurant> data) {
		super(context, resource, data);
		this.resource = resource;
		this.context = context;
		this.data = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		RestaurantResultHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(resource, parent, false);

			holder = new RestaurantResultHolder();
			holder.name = (TextView) row.findViewById(R.id.rest_name);
			holder.rating = (ImageView) row.findViewById(R.id.rest_rating);
			holder.location = (TextView) row.findViewById(R.id.rest_location);
			holder.distance = (TextView) row.findViewById(R.id.rest_dist);
			holder.friend = (TextView) row.findViewById(R.id.rest_friends);
			holder.price = (TextView) row.findViewById(R.id.rest_price);
			holder.foodType = (TextView) row.findViewById(R.id.rest_type);

			row.setTag(holder);
		} else {
			holder = (RestaurantResultHolder) row.getTag();
		}

		Restaurant restaurantResult = data.get(position);
		holder.name.setText((position + 1) + ". " + restaurantResult.getName());
		holder.rating.setImageResource(getImangeIcon(restaurantResult
				.getAverageRating()));
		holder.location.setText(restaurantResult.getAddress());
		holder.distance.setText(restaurantResult.getDistance());
		holder.friend.setText(getFriendVisit(restaurantResult.getReviews()));
		holder.price.setText("Price: "
				+ restaurantResult.getPriceType().toString());
		holder.foodType.setText(getFoodType(restaurantResult.getFoodtypes()));

		return row;
	}

	private CharSequence getFoodType(List<FoodType> foodtypes) {
		String res = "Food Type: ";
		for (FoodType type : foodtypes) {
			res += type.name() + ", ";
		}
		return res.substring(0, res.length() - 2);
	}

	private String getFriendVisit(List<RestaurantReview> reviews) {
		if (reviews == null || reviews.size() == 0)
			return "";
		String res = "";
		res += reviews.size() + " friend(s) reviewed: ";
		for (RestaurantReview review : reviews) {
			res += review.getAuthor() + ", ";
		}
		return res.substring(0, res.length() - 2);
	}

	private int getImangeIcon(int averageRating) {
		int image;
		switch (averageRating) {
		case 0:
			image = R.drawable.rate_0;
			break;
		case 1:
			image = R.drawable.rate_1;
			break;
		case 2:
			image = R.drawable.rate_2;
			break;
		case 3:
			image = R.drawable.rate_3;
			break;
		case 4:
			image = R.drawable.rate_4;
			break;
		case 5:
			image = R.drawable.rate_5;
			break;
		default:
			image = R.drawable.nothing;
			break;
		}
		return image;
	}

	static class RestaurantResultHolder {
		TextView name;
		ImageView rating;
		TextView location;
		TextView distance;
		TextView friend;
		TextView price;
		TextView foodType;
	}
}
